var default_status_text = "Bạn đang nghĩ gì ...";

var default_wall_id = $.query.get("wallid");
if (!default_wall_id || default_wall_id === "") default_wall_id = 0;

var deleteThumb = function(me)
{
    me = $(me).parent();
    me.remove();
};

var deleteThumb2 = function(me)
{
    $(me).remove();
};

var setThumb = function(dvThumb, fileId)
{
    $('#' + dvThumb).append("<div class='image-border' align='center'><input type='checkbox' checked='checked' style='float:left; display:none;' class='attach_files' name='attach_files[]' value='" + fileId + "' /><img ondblclick='deleteThumb(this);' title='Kích đúp chuột để xóa' class='image-thumb' border='0' file-id='" + fileId + "' src= 'file/thumb/max/500/500/" + fileId + ".jpg' align='left' /></div>");
};

var setThumb2 = function(dvThumb, fileId, oFile)
{
    $('#' + dvThumb).append("<div ondblclick='deleteThumb2(this);' title='Kích đúp chuột để xóa' class='image-border' align='center'><input type='checkbox' checked='checked' style='float:left; display:none;' class='attach_files' name='attach_files2[]' value='" + fileId + "' /><img border='0' file-id='" + fileId + "' src= 'resources/images/attach.png' align='left' />" + oFile.name + "</div>");
};

var link_update_status_click = function(me)
{
    me = $(me);
    me.css("color", "black");
    me.next().css("color", "#3B5998");
    me.next().next().css("color", "#3B5998");
    me.parents("#form-status").find("#txtFeed").focus();    
    me.parents("#form-status").find("#file-panel").hide();
    me.parents("#form-status").find("#file-panel2").hide();
    me.parents("#form-status").find(".nub").css("margin-left", 5);
    return false;
};

var link_add_file_click = function(me)
{
    me = $(me);
    me.css("color", "black");
    me.prev().css("color", "#3B5998");
    me.next().css("color", "#3B5998");
    me.parents("#form-status").find("#txtFeed").focus();
    me.parents("#form-status").find("#file-panel2").hide();
    me.parents("#form-status").find("#file-panel").show();
    me.parents("#form-status").find(".nub").css("margin-left", 127);
    return false;
};

var link_add_file2_click = function(me)
{
    me = $(me);
    me.css("color", "black");
    me.prev().css("color", "#3B5998");
    me.prev().prev().css("color", "#3B5998");
    me.parents("#form-status").find("#txtFeed").focus();
    me.parents("#form-status").find("#file-panel").hide();
    me.parents("#form-status").find("#file-panel2").show();
    me.parents("#form-status").find(".nub").css("margin-left", 250);
    return false;
};

var feed_status_focus = function(me)
{
    me = $(me);
    if (me.val() == default_status_text) me.val("");

    me.parents("#form-status").find("#button-panel").show();
    if (me.height() < 32)
        me.height(32);

    if (me.attr("binder") != "true")
    {
        me.attr("binder", "true");

        me.blur(function(){
            if ($(this).val() == "")
            {
                me.val(default_status_text);
                //me.parents("#form-status").find("#button-panel").hide();
                //me.height(22);
            }
        });

        me.autoResize({               
            extraSpace : 10
        });

        me.parents("form").submit(function()
        {            
            if ((me.val() === "") || (me.val() === default_status_text))
            {
                me.val('');
                var m1 = $('#file-panel').css('display');
                var m2 = $('#file-panel .file-thumbnail').html();
                if (m1 === "block" && m2 === "")
                {
                    me.focus();
                    return false;
                }
                
                var m3 = $('#file-panel2').css('display');
                var m4 = $('#file-panel2 .file-thumbnail').html();
                if (m3 === "block" && m4 === "")
                {
                    me.focus();
                    return false;
                }
                
                if (m1 === "none" && m3 === "none")
                {
                    me.focus();
                    return false;
                }
            }
            
            $.ajax({
               url : "?call=feed.post.status&wallid=" + default_wall_id,
               type : "post",
               dataType : "json",
               data : $(this).serializeArray(),
               success : function(data)
               {
                   if (data.success)
                   {
                       if (data.share_id) $("#win-share-feed").dialog("close");
                       
                       if (data.load)
                       {
                           $.ajax(
                           {
                               url : "?load=feed.tpl.feed-entry",
                               data : {id : data.feed_id},
                               success : function(d){                           
                                   $('.feed-entry').first().before("<div class='feed-entry' style='display:none;' feed-id='" + data.feed_id + "'>" + d + "</div>");
                                   $('.feed-entry').first().show(100);
                               }
                           });
                       }

                       me.val("");
                       me.blur();
                       me.parents(".feed-input").find(".file-thumbnail").html("");
                   }else alert(data.msg);
               }
            });
            return false;
        });
    }
};

var feed_like_click = function(me)
{
    me = $(me);
    var feedId = me.attr("feed-id");
    $.ajax({
        url : "?call=feed.post.like",
        type : "post",
        dataType : "json",
        data : {"id" : feedId},
        success : function(data){
            if (data.success)
            {
                if (me.html().indexOf("Bỏ thích") >=0 )
                {
                    $.ajax({
                        url : "?load=feed.tpl.get-like-count",
                        type : "post",
                        data : {"id" : feedId},
                        success : function(d){
                            me.next().find("#count").html(d);
                            me.next().show();
                        }
                    });
                    me.html("Thích");
                    //me.next().hide();
                }
                else 
                {
                    me.html("Bỏ thích");
                    $.ajax({
                        url : "?load=feed.tpl.get-like-count",
                        type : "post",
                        data : {"id" : feedId},
                        success : function(d){
                            me.next().find("#count").html(d);
                            me.next().show();
                        }
                    });
                }
            }else alert(data.msg);
        }
    });

    return false;
}

var input_comment_focus = function(me)
{
    me = $(me);
    if (me.val() == "Viết bình luận ...") me.val("");
    me.parent().prev().show();
    me.parent().width(375);
    me.autoResize({extraSpace : 10});

    if (me.attr("binder") == "true") return true;

    me.attr("binder", "true");

    me.keyup(function (event) {
        if (event.keyCode == 13 && (event.ctrlKey || event.altKey)) {            
           var content = this.value;
           var caret = $.getCaret(this);
           this.value = content.substring(0,caret)+
                         "\n"+content.substring(caret,content.length);
           event.stopPropagation();
        }
    });

    me.keydown(function(e){
        me = $(this);
        
        if (e.keyCode == 13 && !e.shiftKey && !e.ctrlKey && !e.altKey)
        {
            e.preventDefault();

            if (me.val() == "" || me.val() == "Viết bình luận ...") return;

            $.ajax({
                url : "?call=feed.post.comment",
                type : "post",
                data : {"id" : me.attr("feed-id"), "wallid" : default_wall_id , "comment" : me.val()},
                success : function(id){
                    if (id == 0) alert("Bạn chưa đăng nhập hệ thống");
                    else 
                    {
                        $.ajax({
                            url : "?load=feed.tpl.comment-entry",
                            data : {"id" : id},
                            success : function(data){
                                me.parents('#comment-form').before(data);
                            }
                        });
                    }
                }
            });

            me.val("Viết bình luận ...");
            me.height(25);
            me.parent().prev().hide();
            me.parent().width("100%");
            me.blur();
        }
    });
}

var input_comment_blur = function(me)
{
    me = $(me);
    if (me.val() == "")
    {
        me.val("Viết bình luận ...");
        me.height(25);
    }
    me.parent().prev().hide();
    me.parent().width("100%");
}

var link_comment_click = function(me)
{
    me = $(me).parents('#feed_entry').find("#comment-form");
    me.show();
    me.find("#input-comment").focus();
    return false;
}

var view_all_comment = function(me)
{
    me = $(me);

    $.ajax({
        url : "?load=feed.tpl.more-comment",
        data : {"id" : me.attr("feed-id")},
        success : function(data){
            me = me.parents('#div-view-all-comment');
            me.after(data);
            me.hide();
        }
    });
}

var link_share_click = function(me)
{
    me = $(me);
    
    $('#win-share-feed').remove();
    
    $('.win-loading').remove();
    
    var winload = $('<div class="win-loading" title="Đang tải">Đang tải ... </div>');
    winload.dialog({
        height: 100,
        width : 200,
        resizable : false,
        modal: true,
        position : "center",
        autoOpen : true
    });
    
    var win = $('<div id="win-share-feed" title="Chia sẻ ... ">Đang tải ... </div>');

    $.ajax({
        url : "?load=feed.tpl.share&wallid=" + default_wall_id + "&id=" + me.attr("feed-id"),
        success : function(data){
            win.html(data);
        }
    });

    win.dialog({
        height: $.isIE() ? 100 : "auto",
        width : $.isIE() ? 530 : "auto",
        resizable : false,
        autoResize:true,
        modal: true,
        position : "center",
        autoOpen : false
    });

    return false;
}

var image_slide_on_load = function(me)
{
    me = $(me);
    var win = me.parents('#win-view-image');
    if ($.isIE()) win.dialog({"width" : me.width() + 150 < 570  ? 570 : me.width() + 150});
    win.dialog({"position" : "center"});
    
    $('.win-loading').dialog("close");
    win.dialog("open");
}

var image_thumb_click = function(me)
{
    me = $(me);
    
    $('#win-view-image').remove();
    $('.win-loading').remove();
    
    var winload = $('<div class="win-loading" title="Đang tải">Đang tải ... </div>');
    winload.dialog({
        height: 100,
        width : 200,
        resizable : false,
        modal: true,
        position : "center",
        autoOpen : true
    });
    
    var win = $('<div id="win-view-image" style="overflow: hidden;" title="Chia sẻ ảnh ... ">Đang tải ... </div>');

    if (me.attr("type") == 1)
    {
        $.ajax({
            url : "?load=feed.tpl.view-file&wallid=" + default_wall_id + "&file_id=" + me.attr("file-id"),
            success : function(data){
                win.html(data);
            }
        });
    }
    else
    {
        $.ajax({
            url : "?load=feed.tpl.view-image&wallid=" + default_wall_id + "&id=" + me.attr("feed-id") + "&imageid=" + me.attr("file-id"),
            success : function(data){
                win.html(data);
            }
        });
    }
    
    
    win.dialog({
        height: $.isIE() ? 200 : "auto",
        width : $.isIE() ? 200 : "auto",
        resizable : false,
        autoResize:true,
        modal: true,
        position : [-1000,-1000],
        autoOpen : false
    });

    return false;
}

var loading_feed = false;
var auto_load_feed = function(isInit){
    if ((($(document).height() - $(window).height()) - $(window).scrollTop() < 100 && loading_feed == false) || (isInit == true))
    {
        loading_feed = true;
        var maxid = $('.feed-entry').last();
        var feedType = maxid.attr("feed-type");
        if (!feedType) feedType = 0;
        
        if (maxid) maxid = maxid.attr("feed-id");

        $.ajax({
            url : "?load=feed.tpl.more-feed",
            data : {"id" : maxid, "wallid" : default_wall_id, "feed_type" : feedType},
            success : function(data){
                $('.feed-entry').last().after(data);

                $('.feed-entry[feed-id!=0]').each(function(){
                    if ($(this).html() == "")
                        $(this).load("?load=feed.tpl.feed-entry&wallid=" + default_wall_id + "&id=" + $(this).attr("feed-id"));
                });

                if (data != "") loading_feed = false;
            }
        });
    }
}

$(window).scroll(auto_load_feed);

var youtube_player = function(me)
{
    var me = $(me);
    var tmp = '<embed width="397" height="298" flashvars="width=397&amp;height=298" wmode="opaque" salign="tl" allowscriptaccess="never" allowfullscreen="true" scale="scale" quality="high" bgcolor="#FFFFFF" name="swf_ul352q_1" id="swf_ul352q_1" style="" src="https://www.youtube.com/v/' + me.attr("vid") + '?version=3&amp;autohide=1&amp;autoplay=1" type="application/x-shockwave-flash">';
    //var tmp = '<object width="480" height="295" type="application/x-shockwave-flash" id="ytPlayer" data="http://www.youtube.com/v/' + me.attr("vid") + '?version=3&amp;enablejsapi=1&amp;playerapiid=player1"><param name="allowScriptAccess" value="always"></object>';
    me.parents("#youtube-player").html(tmp);
    return false;
}

var feed_config_over = function(me)
{
    me = $(me);
    me.find(".feed-config").show();
}

var feed_config_out = function(me)
{
    me = $(me);
    me.find(".feed-config").hide();
}

var comment_config_over = function(me)
{
    me = $(me);
    me.find(".comment-config").show();
}

var comment_config_out = function(me)
{
    me = $(me);
    me.find(".comment-config").hide();
}

var feed_avatar_over = function(me)
{
    me = $(me);
    //me.find(".change-picture").show();
}

var feed_avatar_out = function(me)
{
    me = $(me);
    //me.find(".change-picture").hide();
}

var comment_delete_click = function(me)
{
    if (confirm('Bạn có muốn xóa bài viết này'))
    {
        me = $(me);
        $.ajax({
            url : "?call=feed.post.delete",
            dataType : "json",
            type : "post",
            data : {"id" : me.attr("feed-id")},
            success : function(data){
                if(data.success)
                {
                    var f = me.parents("#dv-comment-item");
                    if (f.attr('deleted') == 1 || f.attr('issys') == 0) f.remove();
                    else 
                    {
                        f.attr('deleted', 1);
                        f.css("border-right", "solid 2px red");
                        f.css("padding-right", "1px");
                    }
                }
                else alert(data.msg);
            }
        });
    }
}

var feed_delete_click = function(me)
{
    if (confirm('Bạn có muốn xóa bài viết này'))
    {
        me = $(me);
        $.ajax({
            url : "?call=feed.post.delete",
            dataType : "json",
            type : "post",
            data : {"id" : me.attr("feed-id")},
            success : function(data){
                if(data.success)
                {
                    var f = me.parents("#feed_entry");
                    if (f.attr('deleted') == 1 || f.attr('issys') == 0) f.parent().remove();
                    else 
                    {
                        f.attr('deleted', 1);
                        f.css("border-right", "solid 2px red");
                        f.css("padding-right", "1px");
                    }
                }
                else alert(data.msg);
            }
        });
    }
}

var show_more = function(me)
{
    me = $(me);
    me.hide();
    me.next().show("slow");
    return false;
}

var show_less = function(me)
{
    me = $(me);
    me.parent().prev().show();
    me.parent().hide();
    return false;
}

$(function(){
   $('.load').each(function(){
      var me = $(this);
      me.load("?load=" + me.attr("path"));
   });
    
    if (top !== self)
    {
        $('#spFeedLogout').hide();
    }
    else
    {
        //$('#spFeedRefresh').hide();
    }
   auto_load_feed(true);   
});
